import requests
import re
import time


class All_xs(object):
    def __init__(self):
        self.url = 'http://www.ibiqu.org/130_130510/'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
        }

    def resp_get(self):
        response = requests.get(self.url, headers=self.headers)
        if response.status_code == 200:
            return response.text
        else:
            return None

    def var_get(self, response):
        r1 = re.findall(r'''<a href="(.*?)">(.*?)</a>''', response)
        r2 = r1[17:]
        for data in r2:
            a, n = data
            self.My_get(a, n)
            time.sleep(1)

    def My_get(self, a, n):
        response = requests.get(a, headers=self.headers)
        r1 = re.findall(r'<p>(.*)</p>', response.text)
        for data in r1:
            data1 = data.replace('</p><p>', '\n').strip()
            data2 = data1.replace('，', '').rstrip()
            with open(f'./小说/{n}.txt', 'w', encoding='utf-8') as f:
                f.write(data2)
        print(f'{n}---保存成功')


    def main(self):
        self.var_get(self.resp_get())


if __name__ == '__main__':
    All = All_xs()
    All.main()
